/* 
 * File:   GUI.cpp
 * Author: matt
 * 
 * Created on 16 May 2010, 18:42
 */

#include "GUI.h"

GUI::GUI() {
    use_cam = false;
    layout = new QGridLayout(this);
    display = new QImageDisplay();
    imageloader = new QImageLoader();
    timeslice = new QTimeSlice();
    camera = new QCamera();
    imagesaver = new QImageSaver();
    timer = new QTimer(this);
    cam_enable = new QCheckBox("Use Webcam Input");
    cam_enable->setChecked(false);
    layout->addWidget(cam_enable);
    layout->addWidget(display);
    layout->addWidget(imageloader);
    layout->addWidget(timeslice);
    layout->addWidget(imagesaver);

    //connect(timer, SIGNAL(timeout()), camera, SLOT(getImage()));
    //connect(camera,SIGNAL(newImage(QImage)),display,SLOT(setImage(QImage)));
    timer->start(5);

    connect(cam_enable, SIGNAL(clicked(bool)), this, SLOT(setWebcam(bool)));
    connect(imageloader, SIGNAL(newImage(QImage)), timeslice, SLOT(setImage(QImage)));
    //connect(camera,SIGNAL(newImage(QImage)),timeslice,SLOT(setImage(QImage)));
    connect(timeslice, SIGNAL(newImage(QImage)), display, SLOT(setImage(QImage)));
    connect(timeslice, SIGNAL(newImage(QImage)), imagesaver, SLOT(setImage(QImage)));
}

GUI::~GUI() {
}

void GUI::setWebcam(bool) {
    use_cam = !use_cam;
    if (use_cam == true) {
        cerr<<"Enabling webcam"<<endl;
        disconnect(imageloader, SIGNAL(newImage(QImage)), timeslice, SLOT(setImage(QImage)));
        connect(timer, SIGNAL(timeout()), camera, SLOT(getImage()));
        connect(camera, SIGNAL(newImage(QImage)), timeslice, SLOT(setImage(QImage)));
    }
    if (use_cam == false) {
        cerr<<"Disabling webcam"<<endl;
        connect(imageloader, SIGNAL(newImage(QImage)), timeslice, SLOT(setImage(QImage)));
        disconnect(timer, SIGNAL(timeout()), camera, SLOT(getImage()));
        disconnect(camera, SIGNAL(newImage(QImage)), timeslice, SLOT(setImage(QImage)));
    }
}
